package android.slc.user.ui.dept.vmbox

import android.slc.adapter.LoadMoreDataFormat3
import android.slc.adapter.PageModel
import android.slc.adapter.SelectBox
import android.slc.adapter.SelectUtils
import android.slc.appbase.api.slc.callback.SlcObserver
import android.slc.appbase.repository.remote.DataTransformUtils
import android.slc.appbase.repository.remote.SelectTransformUtils
import android.slc.appbase.ui.vd.wrapper.BaseListPageWrapper
import android.slc.appbase.ui.vd.wrapper.IBaseListPageWrapper
import android.slc.extras.component.tree.ui.TreeListMultipleVmBox
import android.slc.extras.user.entity.SysDept
import android.slc.extras.user.repository.remote.DeptServiceRepository
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 *
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/06 下午 2:42
 *
 */
class DeptListMultipleVmBox : TreeListMultipleVmBox<SysDept, SelectBox<SysDept>>(),
    IBaseListPageWrapper<SelectBox<SysDept>> {
    private val baseListPageWrapper: BaseListPageWrapper<SelectBox<SysDept>>

    init {
        baseListPageWrapper = object : BaseListPageWrapper<SelectBox<SysDept>>(this) {

            override fun onLoadMore() {
                this@DeptListMultipleVmBox.loadMore()
            }
        }
    }

    override fun getDataListSlcObserver(): SlcObserver<PageModel<SelectBox<SysDept>>> {
        return baseListPageWrapper.dataListSlcObserver
    }

    override fun getLoadMoreDataFormat3(): LoadMoreDataFormat3 {
        return baseListPageWrapper.loadMoreDataFormat3
    }

    override fun refresh() {
        baseListPageWrapper.refresh()
    }

    override fun onSucceed(data: PageModel<SelectBox<SysDept>>) {
        this.loadMoreDataFormat3.formatSimple(dataList, data)
        dataListOf.notifyChange()
    }

    override fun onError(errorCode: Int, errorMessage: String?) {
        onFailed(errorCode, errorMessage)
        this.loadMoreDataFormat3.loadMoreFail()
    }

    final override fun loadMore() {
        super.loadMore()
    }

    override fun onLoadMore() {
        loadMoreTree()
    }

    private fun getTreeId(): Long {
        return try {
            mTreeId.toLong()
        } catch (e: NumberFormatException) {
            0
        }
    }

    override fun loadMoreTree() {
        DeptServiceRepository.page(getTreeId(), loadMoreDataFormat3.offset)
            .map(SelectTransformUtils.transitionSelectBoxPageMap())
            .map {
                if (it.isSuccess) {
                    SelectUtils.fillSelect(
                        DataTransformUtils.itemTransitionTargetList(
                            getSelectTreeList(
                                mSelectNotParentList,
                                onSelectTreeListByListener = null
                            )
                        ), it.data.records
                    )
                }
                return@map it
            }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .to(bindToLifecycle())
            .subscribe(dataListSlcObserver)
    }

}